/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package TimeRecording.reporting;

import TimeRecording.tables.Employee;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Map;
import javax.persistence.EntityManager;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author david
 */
public class EndOfMonthSummary {

    public static class Detail {
        
        private String name;
        private double granted;
        private double approved;
        private double notApproved;
        private double taken;
        private double sickness;
        private double remaining;

        public Detail() {
        }

        public Detail(String name, double granted, double approved,
                double notApproved, double taken, double remaining,
                double sickness) {

            this.name = name;
            this.granted = granted;
            this.approved = approved;
            this.notApproved = notApproved;
            this.taken = taken;
            this.remaining = remaining;
            this.sickness = sickness;
        }
        
        public double getGranted() {
            return granted;
        }

        public void setGranted(double granted) {
            this.granted = granted;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public double getApproved() {
            return approved;
        }

        public void setApproved(double approved) {
            this.approved = approved;
        }

        public double getNotApproved() {
            return notApproved;
        }

        public void setNotApproved(double notApproved) {
            this.notApproved = notApproved;
        }

        public double getRemaining() {
            return remaining;
        }

        public void setRemaining(double remaining) {
            this.remaining = remaining;
        }

        public double getSickness() {
            return sickness;
        }

        public void setSickness(double sickness) {
            this.sickness = sickness;
        }

        public double getTaken() {
            return taken;
        }

        public void setTaken(double taken) {
            this.taken = taken;
        }
    }

    @SuppressWarnings("unchecked")
    public void viewReport(EntityManager entityManager) {

        ArrayList<Detail> data = new ArrayList<Detail>();

        JasperReport template;
        JasperPrint report;

        try {
            List<Employee> employees = Employee.getEmployees(entityManager);
            for (Employee employee : employees) {
                data.add(new Detail(employee.getFriendlyName(),
                        employee.getWorkingPatternsMaster().getAnnualLeaveGranted(),
                        employee.getWorkingPatternsMaster().getAnnualLeaveApproved(),
                        employee.getWorkingPatternsMaster().getAnnualLeaveUnapproved(),
                        employee.getWorkingPatternsMaster().getAnnualLeaveTaken(),
                        employee.getWorkingPatternsMaster().getAnnualLeaveRemaining(),
                        employee.getSickness()));
            }

            Map parameters = new ReportingUtilities().getBasicParameters();
            
            template = (JasperReport)JRLoader.loadObject(
                    getClass().getResource("/reports/employee/MonthlyAbsences.jasper"));
            
            report = JasperFillManager.fillReport(
                    template, parameters,
                    new JRBeanCollectionDataSource(data));

            JasperViewer.viewReport(report, false);

        } catch (JRException ex) {
            Logger.getLogger(EndOfMonthSummary.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
